/**
This file is part of Gel2DE.

    Gel2DE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gel2DE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Gel2DE.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (C) 2012, University of Bergen
*/

#include "ProjectConfiguration.h"
#include <direct.h>

ProjectConfiguration::ProjectConfiguration(void)
{
	m_8bit = false;
}

ProjectConfiguration::~ProjectConfiguration(void)
{
}

void ProjectConfiguration::Clear()
{
	for(std::vector<std::string*>::iterator i = m_excludedFiles->begin(); i != m_excludedFiles->end(); ++i)
	{
		delete (*i);
	}
	delete m_excludedFiles;
}

std::string ProjectConfiguration::getProjectFile()
{
	return m_currentFile;
}

void ProjectConfiguration::FromXML(std::string aProjectXMLFile)
{

	TiXmlDocument doc;
	doc.LoadFile(aProjectXMLFile.c_str());


	m_currentFile = aProjectXMLFile;

	const size_t last_slash_idx = aProjectXMLFile.rfind('\\');
	std::string directory;
	if(std::string::npos != last_slash_idx)
	{
		directory = aProjectXMLFile.substr(0, last_slash_idx);
	}

	_chdir(directory.c_str());

	m_excludedFiles = new std::vector<std::string*>();

	TiXmlNode* node;
	
	for(node = doc.FirstChild(); node != 0; node = node->NextSibling()){
		std::string tagName = node->Value();
		if(node->Type() == TiXmlNode::TINYXML_ELEMENT && tagName == "project") {
			TiXmlNode* projNode;
			for(projNode = node->FirstChild(); projNode != 0; projNode = projNode->NextSibling()){
				if(projNode->Type() == TiXmlNode::TINYXML_ELEMENT) {
					TiXmlElement *el = (TiXmlElement *)projNode;
					std::string elmName = el->Value();
					if (elmName == "ParameterFile"){
						std::string parameterFileName = el->GetText();
						m_ParameterFile = new gel2de::File(parameterFileName);
					}
					else if (elmName == "RegistrationFile"){
						std::string parameterFileName = el->GetText();
						m_RegistrationFile = new gel2de::File(parameterFileName);
					}
					else if (elmName == "OutputDirectory"){
						std::string parameterFileName = el->GetText();
						m_OutputDataDir = new gel2de::File(parameterFileName);
					}
					else if (elmName == "ExcludedGelImages"){
						TiXmlNode* excludedFileNode;
						for(excludedFileNode = el->FirstChild(); excludedFileNode != 0; excludedFileNode = excludedFileNode->NextSibling())
						{
							if(excludedFileNode->Type() == TiXmlNode::TINYXML_ELEMENT){
								TiXmlElement *exclElm = (TiXmlElement*)excludedFileNode;
								std::string* exludedFile = new std::string(exclElm->GetText());
								m_excludedFiles->push_back(exludedFile);
							}
						}
					}
					else if (elmName == "ColorTransferFunction"){
						std::string ctfFileName = el->GetText();
						m_ctfFile = new gel2de::File(ctfFileName);
					}
					else if (elmName == "BitDepth")
					{
						std::string bitDepth = el->GetText();
						std::string bit8("8");
						std::string bit16("16");
						if (bitDepth == bit8)
						{
							m_8bit = true;
						}
						else if(bitDepth == bit16)
						{
							m_8bit = false;
						}
						else
						{
							//Should not happen!!!
						}
					}
				}
			}
		}
	}
}


void ProjectConfiguration::ToXML(std::string aFileName, std::vector<std::string*>* aVector)
{
	TiXmlDocument doc;
	TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
	TiXmlElement* rootElement = new TiXmlElement("project");
	TiXmlElement* parameterFileElement = new TiXmlElement("ParameterFile");
	TiXmlElement* registrationFileElement = new TiXmlElement("RegistrationFile");
	TiXmlElement* outputDirElement = new TiXmlElement("OutputDirectory");

	parameterFileElement->LinkEndChild(new TiXmlText(getParameterFile()->path().c_str()));
	registrationFileElement->LinkEndChild(new TiXmlText(getRegistrationFile()->path().c_str()));
	outputDirElement->LinkEndChild(new TiXmlText(getOutputDataDir()->path().c_str()));

	rootElement->LinkEndChild(parameterFileElement);
	rootElement->LinkEndChild(registrationFileElement);
	rootElement->LinkEndChild(outputDirElement);

	m_excludedFiles->clear();


	TiXmlElement* excluded = new TiXmlElement("ExcludedGelImages");
	std::vector<std::string*>::iterator i = aVector->begin();
	for(i; i != aVector->end(); ++i)
	{
		TiXmlElement *gelimage = new TiXmlElement("GelImage");
		gelimage->LinkEndChild(new TiXmlText((*(*i)).c_str()));
		excluded->LinkEndChild(gelimage);
		m_excludedFiles->push_back((*i));
	}

	rootElement->LinkEndChild(excluded);

	if(m_ctfFile)
	{
		TiXmlElement* ctfElement = new TiXmlElement("ColorTransferFunction");
		ctfElement->LinkEndChild(new TiXmlText(m_ctfFile->path().c_str()));
		rootElement->LinkEndChild(ctfElement);
	}

	doc.LinkEndChild(decl);
	doc.LinkEndChild(rootElement);
	doc.SaveFile(aFileName.c_str());	
}

gel2de::File* ProjectConfiguration::getOutputDataDir()
{
	return m_OutputDataDir;
}

gel2de::File* ProjectConfiguration::getRegistrationFile()
{
	return m_RegistrationFile;
}

gel2de::File* ProjectConfiguration::getParameterFile()
{
	return m_ParameterFile;
}
//NB: Only reflects what is stored in the current project, not the current state.
std::vector<std::string*>* ProjectConfiguration::getExcludedFilesStoredInProject()
{
	return m_excludedFiles;
}

gel2de::File* ProjectConfiguration::getCtfFile()
{
	return m_ctfFile;
}
